const util = require('../../utils/util.js');
const api = require('../../config/api.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    carModel: '',
    dialogShow: false,
    buttons: [{ text: '确定' }],
    countDownNum: 5,
    timer: '', //定时器
    btnText:'5S后关闭',
    schoolId:'',
    schoolName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (option) {
    var hasLogin = wx.getStorageSync("app.globalData.hasLogin");  
    if (!hasLogin) {
      wx.navigateTo({
        url: '/pages/auth/login/login',
      })
    }else{
      this.setData({
        schoolId:option.schoolId?option.schoolId:'',
        schoolName:option.schoolName?option.schoolName:''
      })
    }

    //console.log(this.data.schoolId+"驾校基本信息"+this.data.schoolName);

    /*
    var that=this;
    var item='buttons[0].text';
    that.setData({
      [item]:that.btnText
    })*/
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //查看条款
  toClause: function () {
    wx.navigateTo({
      url: '../clause/index',
    })
  },
  //下一步
  next: function () {
    var that = this;
    if (that.data.carModel == "") {
      wx.showModal({
        title: '提示',
        content: '请选择学习车型',
        showCancel: false
      })
      return
    }


    //进行保存用户选择的驾考类型
    wx.setStorageSync("carModel",this.data.carModel);
    

    wx.redirectTo({
      url: 'info?carModel=' + this.data.carModel+"&schoolId="+that.data.schoolId+'&schoolName='+that.data.schoolName,
    })
  },
  select: function (event) {
    var type = event.currentTarget.dataset.type;
    var that = this;
    that.setData({
      carModel: type
    });
  },
  openConfirm: function () {
    this.setData({
      dialogShow: true
    })
  },
  tapDialogButton(e) {
    console.log(e.detail.index);//0为取消  1为确认
    var index=e.detail.index;
    if(index==1){//确认
      if(this.data.carModel==''){
        util.showErrorToast('请选择车型');
        return;
      }
      this.setData({
        dialogShow: false
      })
      util.redirect('/pages/signupV3/info?carModel='+this.data.carModel);
      // wx.navigateTo({
      //   url: '/pages/signupV3/info?carModel='+this.data.carModel
      // })
      return
    }
    this.setData({
      dialogShow: false,
      carModel:''
    })
  },
  //取消
  bindCancel(){
      wx.navigateBack({
        delta: 1
      })
  },
  //定时器
  countDown: function () {
    let that = this;
    let countDownNum = 5; //获取倒计时初始值 
    that.setData({
      countDownNum: countDownNum
    })
    that.setData({
      timer: setInterval(function () {
        countDownNum--;
        that.setData({
          countDownNum: countDownNum
        })
        if (countDownNum == 0) {
          clearInterval(that.data.timer);
          //关闭定时器之后，可作其他处理     
          wx.switchTab({
            url: '../home/home',
          })
        }
      }, 1000)

    })
  }

})